<template>
  <div>
    <el-divider content-position="left">新增员工</el-divider>
    <el-form style="width: 400px" ref="form" :rules="rules" :model="form">
      <el-form-item label="姓名:" prop="name">
        <el-input
          v-model="form.name"
          placeholder="请输入员工姓名"
          style="width: 300px"
        ></el-input>
      </el-form-item>
      <el-form-item label="头像:" prop="pic">
        <el-upload
          class="avatar-uploader"
          :action="`${UPLOADURL}/upload`"
          :show-file-list="false"
          :on-success="handleAvatarSuccess"
          :before-upload="beforeAvatarUpload"
        >
          <img v-if="form.pic" :src="form.pic" class="avatar" />
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
      </el-form-item>
      <el-form-item label="生日:" prop="birthday">
        <el-date-picker
          style="width: 300px"
          type="date"
          v-model="form.birthday"
          placeholder="选择日期"
          value-format="yyyy-MM-DD"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item label="所属部门:" prop="department">
        <el-select v-model="form.department" style="width: 300px">
          <el-option
            v-for="item in departmentList"
            :key="item.did"
            :label="item.department"
            :value="item.department"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="性别:" prop="sex">
        <el-select style="width: 300px" v-model="form.sex">
          <el-option
            v-for="item in gender"
            :key="item.gid"
            :value="item.sex"
            :label="item.sex"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="住址:" prop="address">
        <el-input
          v-model="form.address"
          style="width: 300px"
          placeholder="请输入员工住址"
        ></el-input>
      </el-form-item>
      <el-form-item label="联系方式:" prop="tel">
        <el-input
          v-model="form.tel"
          style="width: 300px"
          placeholder="请输入员工联系方式"
        ></el-input>
      </el-form-item>
      <el-form-item class="btn">
        <el-button type="primary" @click="submit">新增</el-button>
        <el-button type="warning" @click="clearForm">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import httpApi from "@/http";
export default {
  data() {
    return {
      gender: [],
      departmentList: [],
      form: {
        name: "",
        pic: "",
        birthday: "",
        sex: "",
        department: "",
        address: "",
        tel: "",
      },
      rules: {
        name: [{ required: true, message: "必填", trigger: "blur" }],
        pic: [{ required: true, message: "必填", trigger: "blur" }],
        birthday: [{ required: true, message: "必填", trigger: "blur" }],
        sex: [{ required: true, message: "必填", trigger: "blur" }],
        department: [{ required: true, message: "必填", trigger: "blur" }],
        address: [{ required: true, message: "必填", trigger: "blur" }],
        tel: [
          {
            required: true,
            message: "请填写格式正确的手机号",
            trigger: "blur",
          },
          {
            pattern: /^\w{3,15}$/,
            message: "3~15位字符，可以包含：数字、字母、下划线",
            trigger: "blur",
          },
        ],
      },
    };
  },
  mounted() {
    this.memberSex();
    this.memberDepartment();
  },
  methods: {
    submit() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          console.log(this.form);
          httpApi.memberApi.memberAdd(this.form).then((res) => {
            if (res.data.code == 200) {
              this.$message({ type: "success", message: "新增员工成功" });
              this.$router.push("/home/member-list");
            } else {
              this.$message({ type: "error", message: "新增员工失败" });
            }
          });
        }
      });
    },
    clearForm() {
      this.$refs.form.resetFields();
    },
    memberSex() {
      httpApi.customerApi.querySex().then((res) => {
        this.gender = res.data.data;
      });
    },
    memberDepartment() {
      httpApi.memberApi.queryDepartment().then((res) => {
        this.departmentList = res.data.data;
      });
    },
    handleAvatarSuccess(res, file) {
      this.form.pic = res.data;
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === "image/jpeg";
      const isLt2M = file.size / 1024 / 1024 < 2;
      if (!isJPG) {
        this.$message.error("上传头像图片只能是 JPG 格式!");
      }
      if (!isLt2M) {
        this.$message.error("上传头像图片大小不能超过 2MB!");
      }
      return isJPG && isLt2M;
    },
  },
};
</script>

<style lang="scss" scoped>
.btn {
  text-align: center;
}
::v-deep .el-form-item__label {
  width: 100px;
  text-align-last: justify;
}
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  border: 1px dashed #dcdfe6;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
::v-deep .cell {
  text-align: center;
}
</style>